package com.zztl.infiniteBand.ui.activity

import android.text.Editable
import android.text.InputFilter
import android.text.InputType
import android.text.TextWatcher
import android.text.method.PasswordTransformationMethod
import android.view.View
import butterknife.OnClick
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.base.BaseMvpActivity
import com.zztl.infiniteBand.presenter.PayPwdPresenter
import com.zztl.infiniteBand.ui.view.CommonItemView2
import com.zztl.infiniteBand.ui.view.dialog.ExitDialog
import com.zztl.infiniteBand.ui.view.mvpview.PayPwdView
import com.zztl.infiniteBand.util.toast
import kotlinx.android.synthetic.main.activity_forget_or_reset_pwd.*
import kotlinx.android.synthetic.main.activity_reset_pay_pwd.*
import java.util.regex.Matcher
import java.util.regex.Pattern

class ResetPayPwdActivity : BaseMvpActivity<PayPwdView, PayPwdPresenter>(), PayPwdView, TextWatcher {


    private var mExitDialog: ExitDialog? = null
    lateinit var mPayPwdPresenter: PayPwdPresenter
    private var isLook = false
    private var isLook2 = false

    override fun setLayoutId(): Int = R.layout.activity_reset_pay_pwd


    override fun initBase() {
        edit_pwd.middleEdit.run {
            filters = arrayOf(InputFilter.LengthFilter(16))
            addTextChangedListener(this@ResetPayPwdActivity)
        }
        edit_twice_pwd.middleEdit.run {
            filters = arrayOf(InputFilter.LengthFilter(16))
            addTextChangedListener(this@ResetPayPwdActivity)
        }

        edit_pwd.setRightImg(R.mipmap.biyanjing)
        edit_twice_pwd.setRightImg(R.mipmap.biyanjing)

        edit_pwd.setItemRightClickListener(object : CommonItemView2.OnItemRightClickListener {
            override fun onClickItemRight(viewID: Int) {
                isLook = !isLook.apply {
                    if (this) {
                        edit_pwd.middleEdit.transformationMethod = null
                        edit_pwd.middleEdit.inputType = InputType.TYPE_CLASS_TEXT
                        edit_pwd.setRightImg(R.mipmap.zhengyanjing)
                    } else {
                        edit_pwd.middleEdit.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
                        edit_pwd.setRightImg(R.mipmap.biyanjing)
                        edit_pwd.middleEdit.transformationMethod = PasswordTransformationMethod.getInstance()
                    }
                }
            }
        })

        edit_twice_pwd.setItemRightClickListener(object : CommonItemView2.OnItemRightClickListener {
            override fun onClickItemRight(viewID: Int) {
                isLook2 = !isLook2.apply {
                    if (this) {
                        edit_twice_pwd.middleEdit.inputType = InputType.TYPE_CLASS_TEXT
                        edit_twice_pwd.setRightImg(R.mipmap.zhengyanjing)
                    } else {
                        edit_twice_pwd.middleEdit.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
                        edit_twice_pwd.setRightImg(R.mipmap.biyanjing)
                    }
                }
            }
        })
        edit_pwd.middleEdit.run {
            inputType = InputType.TYPE_TEXT_FLAG_MULTI_LINE
            setSingleLine(false)
            setHorizontallyScrolling(false)
            transformationMethod = PasswordTransformationMethod.getInstance()
        }
    }


    override fun createPresenter(): PayPwdPresenter {
        mPayPwdPresenter = PayPwdPresenter()
        return mPayPwdPresenter
    }

    override fun afterTextChanged(s: Editable?) {
        s?.let {
            btn_next_step.isEnabled = edit_pwd.middleEditStr.length in 6..16 && edit_twice_pwd.middleEditStr == edit_pwd.middleEditStr
        }
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

    }

    @OnClick(R.id.btn_cacel, R.id.btn_next_step)
    fun click(view: View) {
        when (view.id) {
            R.id.btn_cacel -> showExitDialog()
            R.id.btn_next_step -> {
                //                showActivity(this,VerifyPhoneActivity.class);
                val pwd = edit_pwd.middleEdit.text.toString().trim { it <= ' ' }
                if (hasSpecialChar(pwd)) {
                    toast(getString(R.string.支持6至16位的数字英文字母特殊字符))
                    return
                }
                mPayPwdPresenter!!.setOrAmendPwd(pwd, this)
            }
            else -> {
            }
        }
    }

    private fun hasSpecialChar(str: String): Boolean {
        val regEx = "[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]|\n|\r|\t"
        val pattern = Pattern.compile(regEx)
        val matcher = pattern.matcher(str)
        return matcher.find()

    }

    private fun showExitDialog() {
        if (null == mExitDialog) {
            mExitDialog = ExitDialog.Builder(this).setMessage(getString(R.string.是否放弃修改支付密码))
                    .setAffirmTxt(getString(R.string.是))
                    .setCancelTxt(getString(R.string.否))
                    .setClickListener(object : ExitDialog.Builder.ClickListener {
                        override fun cancel() {

                        }

                        override fun comfirm() {
                            finish()
                        }
                    }).create()
        }
        mExitDialog!!.show()
    }


    override fun setPayPwdSuccess() {
        finish()
//        ToastUtils.show(this, getString(R.string.设置支付密码成功))
        toast(getString(R.string.设置支付密码成功))
    }

    override fun setPayPwdFail() {
        skipActivity(this, VerifyPhoneActivity::class.java)
    }
}
